package cn.afei.cloud.utils;

import org.joda.time.DateTime;
import org.joda.time.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {
private final static Logger log = LoggerFactory.getLogger(DateUtil.class);
    public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);

    //判断选择的日期是否是本周
    public static boolean isThisWeek(long time)
    {
        Calendar calendar = Calendar.getInstance();
        int currentWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.setTime(new Date(time));
        int paramWeek = calendar.get(Calendar.WEEK_OF_YEAR);
        if(paramWeek==currentWeek){
           return true;
        }
        return false;
    }
    //判断选择的日期是否是今天
    public static boolean isToday(long time)
    {
       return isThisTime(time,"yyyy-MM-dd");
    }
    //判断选择的日期是否是本月
    public static boolean isThisMonth(long time)
    {
         return isThisTime(time,"yyyy-MM");
    }
    private static boolean isThisTime(long time,String pattern) {
        Date date = new Date(time);
         SimpleDateFormat sdf = new SimpleDateFormat(pattern);
         String param = sdf.format(date);//参数时间
         String now = sdf.format(new Date());//当前时间
         if(param.equals(now)){
           return true;
         }
         return false;
    }

    public static String TimesToString(long time) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date(time);
         return simpleDateFormat.format(date);
    }



    //时间字符串转化成日期
    public static Date StringToDate(String stringTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateTime = null;
        try {
            dateTime = sdf.parse(stringTime);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateTime;
    }
    //时间字符串转化成日期
    public static Date StringToDateYMD(String stringTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dateTime = null;
        try {
            dateTime = sdf.parse(stringTime);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateTime;
    }
    //时间字符串转化成日期
    public static Date StringToDateHHMMSS(String stringTime){
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        Date dateTime = null;
        try {
            dateTime = sdf.parse(stringTime);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateTime;
    }
    //时间字符串转化成日期
    public static Date StringToDatedd(String stringTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dateTime = null;
        try {
            dateTime = sdf.parse(stringTime);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateTime;
    }
    //时间字符串转化成日期
    public static Date StringToDateYYYYMMDDHH(String stringTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateTime = null;
        try {
            dateTime = sdf.parse(stringTime);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateTime;
    }

    //时间字符串转化成日期
    public static Date StringToMonth(String stringTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date dateTime = null;
        try {
            dateTime = sdf.parse(stringTime);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateTime;
    }

    //日期转化时间字符串
    public static String DateToString(Date dateTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String format = sdf.format(dateTime);
        return format;
    }

    //日期转化时间字符串
    public static String DateToString2(Date dateTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(dateTime);
        return format;
    }
    //日期转化时间字符串
    public static String DateToStringYMD(Date dateTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(dateTime);
        return format;
    }
    //日期转化时间字符串
    public static String DateToStringY_M_D(Date dateTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        String format = sdf.format(dateTime);
        return format;
    }
    //日期转化时间字符串
    public static String DateToStringYMDHMSHI(Date dateTime){
        if(dateTime == null){
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(dateTime);
        return format;
    }

    //日期转化时间字符串
    public static String DateToStringHHMMSS(Date dateTime){
        if(dateTime == null){
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        String format = sdf.format(dateTime);
        return format;
    }

    //判断是否同一天
    public static boolean isSameDay(Date date1, Date date2) {
        if(date1 != null && date2 != null) {
            Calendar cal1 = Calendar.getInstance();
            cal1.setTime(date1);
            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(date2);
            return isSameDay1(cal1, cal2);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }
    public static boolean isSameDay1(Calendar cal1, Calendar cal2) {
        if(cal1 != null && cal2 != null) {
            return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1) && cal1.get(6) == cal2.get(6);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }


    /**     判断当前时间 是不是星期三
     *  （根据传入的参数进行可控）
     *   以及是本月的第几个星期三
     */
    public static int countDate(Date date,String weekCn){
        SimpleDateFormat format = new SimpleDateFormat("EEE");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            String format2 = simpleDateFormat.format(date);
            System.out.println(String.format("%s 这天是%s", format2,format.format(date)));
            if(!format.format(date).equals(weekCn)){
                System.out.println(String.format("%s 这天不是%s", format2,weekCn));
                return 0;
            }
            int day = Integer.parseInt(new SimpleDateFormat("dd").format(date));
            String yearMonth = new SimpleDateFormat("yyyy-MM-").format(date);
            int number = 0;
            for(int i=1;i<=day;i++){
                String dayStr = i + "";
                if(dayStr.length() == 1){
                    dayStr = "0" + dayStr;
                }
                String weekText = format.format(simpleDateFormat.parse(yearMonth + dayStr));
                if(weekText.equals(weekCn)){
                    number++;
                }
            }
            System.out.println(String.format("%s 这天是第%d个%s", format2,number,weekCn));
            return number;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static Integer getCurrentYear() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        Date date = new Date();
        String format = sdf.format(date);
        return Integer.valueOf(format);
    }
    //周一至周五 晚上6点到12点  周六日 早上8点至晚上10点给推送

    public static boolean isTuiSongAddPublish(Date date) {
        boolean flag = false;
        int dayForWeek = 0;
        try {
            dayForWeek = dayForWeek(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String datetime = DateToString2(date);
        if(dayForWeek >= 1 && dayForWeek <= 5) {
            String b_time =  datetime + " 18:00:00";
            String e_time = datetime  + " 23:59:59";
            Date beginTime = StringToDateYYYYMMDDHH(b_time);
            Date endTime = StringToDateYYYYMMDDHH(e_time);
            if(belongCalendar(date,beginTime) && belongCalendar(endTime,date)){
                return true;
            }
        } else  if(dayForWeek >= 6 && dayForWeek <= 7)  {
            String b_time =  datetime + " 08:00:00";
            String e_time = datetime  + " 22:00:00";
            Date beginTime = StringToDateYYYYMMDDHH(b_time);
            Date endTime = StringToDateYYYYMMDDHH(e_time);

            if(belongCalendar(date,beginTime) && belongCalendar(endTime,date)){
                return true;
            }
        }
        return flag;
    }

    /**
     * 判断时间是否在时间段内 开始时间大于结束时间返回true
     * @param beginTime
     * @param endTime
     * @return true
     * Date1.after(Date2),当Date1大于Date2时，返回TRUE，当小于等于时，返回false；
     * Date1.before(Date2)，当Date1小于Date2时，返回TRUE，当大于等于时，返回false；
     * 如果业务数据存在相等的时候，而且相等时也需要做相应的业务判断或处理时，请注意。
     */
    public static boolean belongCalendar(Date beginTime, Date endTime) {

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        if (begin.after(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past, String formatStr) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        String result = format.format(today);
        return result;
    }

    public  static Date afterMintesDate(Date date,Integer min){
        long time = min * 60 * 1000;//min分钟
        Date afterDate = new Date(date .getTime() + time);//30分钟后的时间
        return afterDate;
    }

    //java中计算两个时间相差bai多少分钟,具体代码如下：

    public static Long getDatePoorMin(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // 获得两du个时间的毫秒时zhi间差异
        long diff = endDate.getTime() - nowDate.getTime();
//        // 计算差多少dao天
//        long day = diff / nd;
//        // 计算差多少小时
//        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
//        // 计算差多少秒//输出结果
//        // long sec = diff % nd % nh % nm / ns;
//        return day + "天" + hour + "小时" + min + "分钟";

        return min;
    }

    public static Long  getMinutes(Date end,Date start){

        long min = (end.getTime() - start.getTime()) / (1000 * 60);

        return min;
    }

    public static Long  getDateDays(Date end,Date start){

        long day = (end.getTime() - start.getTime()) / (1000*3600*24);

        return day;
    }


    public static Long getDatePoorDay(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // 获得两du个时间的毫秒时zhi间差异
        long diff = endDate.getTime() - nowDate.getTime();
//        // 计算差多少dao天
        long day = diff / nd;
//        // 计算差多少小时
//        long hour = diff % nd / nh;
        // 计算差多少分钟
//        long min = diff % nd % nh / nm;
//        // 计算差多少秒//输出结果
//        // long sec = diff % nd % nh % nm / ns;
//        return day + "天" + hour + "小时" + min + "分钟";

        return day;
    }

    /**
     * 秒转换小时-分-秒analytics/util/DateUtil.java
     *
     * @param seconds 秒为单位 比如..600秒
     * @return 比如...2小时3分钟52秒
     */
    public static String secToTime(int seconds) {
        int minute = (seconds) / 60;
        int second = (seconds - minute * 60);

        StringBuffer sb = new StringBuffer();

        if (minute > 0) {
            if(minute < 10){
                sb.append("0" + minute + ":");
            }else {
                sb.append(minute + ":");
            }
        }
        if (second > 0) {
            if(second < 10){
                sb.append("0" + second);
            }else {
                sb.append(second);
            }
        }
        if (second == 0) {
//            sb.append("<1秒");
            sb.append("00");
        }
        return sb.toString();
    }

    public static Long getSecondsNextEarlyMorning() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, 1);
        // 改成这样就好了
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
    }

    /**
     * * 判断当前日期是星期几<br>
     *     * <br>     *

     * @return dayForWeek 判断结果<br>
     *     * @Exception 发生异常<br>     */
    public static int dayForWeek(Date date) throws Exception {

        Calendar c = Calendar.getInstance();
        c.setTime(date);  int dayForWeek = 0;
        if(c.get(Calendar.DAY_OF_WEEK) == 1){
            dayForWeek = 7;
        }else{
            dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
        }  return dayForWeek;
    }

    public static  String getDateIntervalForWeek(Date date){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        System.out.println("要计算日期为:"+ sdf.format(cal.getTime())); //输出要计算日期
        //判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        if(1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.MONDAY);//设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        int day = cal.get(Calendar.DAY_OF_WEEK);//获得当前日期是一个星期的第几天
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek()-day);//根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
//      System.out.println("所在周星期一的日期："+sdf.format(cal.getTime()));
        String value = sdf.format(cal.getTime());
        cal.add(Calendar.DATE, 6);
        value +=  "至" + sdf.format(cal.getTime());
        System.out.println(value);
        return value;
    }

    public static  String getMonthLastDay(String value){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date = format.parse(value);
            Date dt = new Date();
            //获取当前月最后一天
            Calendar ca = Calendar.getInstance();
            ca.setTime(date);
            ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
            String last = format.format(ca.getTime());
            System.out.println("===============last:"+last);

            return last;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static  String getMonthFristDay(){
        try {
            //获取前一个月第一天
            Calendar calendar1 = Calendar.getInstance();
            calendar1.add(Calendar.MONTH, -1);
            calendar1.set(Calendar.DAY_OF_MONTH,1);
            String firstDay = sdf.format(calendar1.getTime());
            return firstDay;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static  String getMonthLastDay(){
        try {
            //获取前一个月第一天
            Calendar calendar2 = Calendar.getInstance();
            calendar2.set(Calendar.DAY_OF_MONTH, 0);
            String lastDay = sdf.format(calendar2.getTime());
            return lastDay;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static  String getMonthFristDay (String value){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date = format.parse(value);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.MONTH, 0);
            c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
            String first = format.format(c.getTime());
            System.out.println("===============first:"+first);

            return first;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static Date DateAddCountDay(Date sDate,Integer day){
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(sDate);
            c.add(Calendar.DAY_OF_MONTH, day);         //利用Calendar 实现 Date日期+1天
            sDate = c.getTime();
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            return sDate;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * -2:前天             -1：昨天            0：今天             1：明天             2：后天
     * @param startDate
     * @return
     */
    public static int getDayString(Date startDate){
        int offSet = Calendar.getInstance().getTimeZone().getRawOffset();
        long today = (System.currentTimeMillis()+offSet)/86400000;
        long start = (startDate.getTime()+offSet)/86400000;
        return (int) (start-today);
    }

    /**
     * 转换日期格式MM.dd
     * @param startDate
     * @return
     */
    public static String DateToStringMMDD (Date startDate){
        String f = new SimpleDateFormat("MM.dd").format(startDate);
        return f;
    }

    /**
     * 获取当天剩余多少秒
     * @return
     */
    public static Long getSecondsLeftInTheDay (){
        //方法二
        DateTime dateTime = new DateTime().millisOfDay().withMaximumValue();
//        long millSeconds2 = new Duration(new DateTime(), dateTime).getMillis();
        long count = new Duration(new DateTime(), dateTime).getStandardSeconds();
//        System.out.println("当天剩余毫秒2：" + millSeconds2);
        System.out.println("当天剩余秒2：" + count);
        return count;
    }

    //日期转化时间字符串
    public static String DateToStringYMDHMIS(Date dateTime){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = sdf.format(dateTime);
        return format;
    }

    /**
     * 获取过去或者未来 任意天内的日期数组
     * @param intervals      intervals天内
     * @return              日期数组
     */
    public static ArrayList<String> test(int intervals ) {
        ArrayList<String> pastDaysList = new ArrayList<>();
        ArrayList<String> fetureDaysList = new ArrayList<>();
        for (int i = 0; i <intervals; i++) {
            pastDaysList.add(getPastDate(i));
            fetureDaysList.add(getFetureDate(i));
        }
        return fetureDaysList;
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
//        log.info(null, result);
        return result;
    }

    /**
     * 获取未来 第 past 天的日期
     * @param past
     * @return
     */
    public static String getFetureDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
//        Log.e(null, result);
        return result;
    }
    public static Integer dateToWeek(String datetime) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        Integer[] weekDays = { 7, 1, 2, 3, 4, 5, 6 };
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        Date datet = null;
        try {
            datet = f.parse(datetime);
            cal.setTime(datet);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
        if(w < 0) {
            w = 0;
        }
        return weekDays[w];
    }

    /**
     * 天数相加
     * @param date
     * @param day
     * @return
     */
    private static Date addDate(Date date,  long day) {
        long time = date.getTime(); // 得到指定日期的毫秒数
        day = day * 24 * 60 * 60 * 1000; // 要加上的天数转换成毫秒数
        time += day; // 相加得到新的毫秒数
        return new Date(time); // 将毫秒数转换成日期
    }
    /**
     * 格式化时间
     * @return
     */
    public static String formatTime(long time) {
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        return df.format(new Date(time));
    }

    public static Date getBeginDayOfLastWeek() {
        Date date = new Date();
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        cal.add(Calendar.DATE, 2 - dayofweek - 7);
        return getDayStartTime(cal.getTime());
    }


// 获取上周的结束时间
    public static Date getEndDayOfLastWeek() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(getBeginDayOfLastWeek());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        Date weekEndSta = cal.getTime();
        return getDayEndTime(weekEndSta);

    }



// 获取某个日期的开始时间
    public static Timestamp getDayStartTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if(null != d) {
            calendar.setTime(d);
        }
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());

    }


// 获取某个日期的结束时间

    public static Timestamp getDayEndTime(Date d) {
        Calendar calendar = Calendar.getInstance();
        if(null != d) {
            calendar.setTime(d);
        }
        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH), 23, 59, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());

    }

    //根据当前日期获取上周
    public static String getLastTimeInterval() {
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;
        int offset1 = 1 - dayOfWeek;
        int offset2 = 7 - dayOfWeek;
        calendar1.add(Calendar.DATE, offset1 - 7);
        calendar2.add(Calendar.DATE, offset2 - 7);
        // System.out.println(sdf.format(calendar1.getTime()));// last Monday
        String lastBeginDate = sdf.format(calendar1.getTime());
        // System.out.println(sdf.format(calendar2.getTime()));// last Sunday
        String lastEndDate = sdf.format(calendar2.getTime());
        return lastBeginDate + "," + lastEndDate;
    }

    //根据当前日期获取本周
    public static String getTimeInterval(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // System.out.println("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        String imptimeBegin = sdf.format(cal.getTime());
        // System.out.println("所在周星期一的日期：" + imptimeBegin);
        cal.add(Calendar.DATE, 6);
        String imptimeEnd = sdf.format(cal.getTime());
        // System.out.println("所在周星期日的日期：" + imptimeEnd);
        return imptimeBegin + "," + imptimeEnd;
    }

    /**
     * 获取上周周几的日期,默认一周从周一开始
     * @param dayOfWeek
     * @param weekOffset
     * @return
     */
    public static Date getDayOfWeek(int dayOfWeek,int weekOffset){
        return getDayOfWeek(Calendar.MONDAY,dayOfWeek,weekOffset);
    }

    /**
     * 获取上(下)周周几的日期
     * @param firstDayOfWeek {@link Calendar}
     * 值范围 <code>SUNDAY</code>,
     * <code>MONDAY</code>, <code>TUESDAY</code>, <code>WEDNESDAY</code>,
     * <code>THURSDAY</code>, <code>FRIDAY</code>, and <code>SATURDAY</code>
     * @param dayOfWeek {@link Calendar} 周几
     * @param weekOffset  周偏移，上周为-1，本周为0，下周为1，以此类推
     * @return
     */
    public static Date getDayOfWeek(int firstDayOfWeek,int dayOfWeek,int weekOffset){
        if(dayOfWeek>Calendar.SATURDAY || dayOfWeek<Calendar.SUNDAY){
            return null;
        }
        if(firstDayOfWeek>Calendar.SATURDAY || firstDayOfWeek < Calendar.SUNDAY){
            return null;
        }
        Calendar date=Calendar.getInstance(Locale.CHINA);
        date.setFirstDayOfWeek(firstDayOfWeek);
        //周数减一，即上周
        date.add(Calendar.WEEK_OF_MONTH,weekOffset);
        //日子设为周几
        date.set(Calendar.DAY_OF_WEEK, dayOfWeek);
        //时分秒全部置0
        date.set(Calendar.HOUR_OF_DAY, 0);
        date.set(Calendar.MINUTE, 0);
        date.set(Calendar.SECOND, 0);
        date.set(Calendar.MILLISECOND, 0);
        return date.getTime();
    }
    public static String getStringWeek(Date date) {
        String week = "";
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int weekday = c.get(Calendar.DAY_OF_WEEK);
        if (weekday == 1) {
            week = "周日";
        } else if (weekday == 2) {
            week = "周一";
        } else if (weekday == 3) {
            week = "周二";
        } else if (weekday == 4) {
            week = "周三";
        } else if (weekday == 5) {
            week = "周四";
        } else if (weekday == 6) {
            week = "周五";
        } else if (weekday == 7) {
            week = "周六";
        }
        return week;
    }

    public static int getIntWeek() {
        Integer week = 0;
        Date today = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        int weekday = c.get(Calendar.DAY_OF_WEEK);
        if (weekday == 1) {
            week = 7;
        } else if (weekday == 2) {
            week = 1;
        } else if (weekday == 3) {
            week = 2;
        } else if (weekday == 4) {
            week = 3;
        } else if (weekday == 5) {
            week = 4;
        } else if (weekday == 6) {
            week = 5;
        } else if (weekday == 7) {
            week = 6;
        }
        return week;
    }

    public static  Date getDayAdd(int day){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        System.out.println("当前日期:"+sf.format(c.getTime()));
        c.add(Calendar.DAY_OF_MONTH, day);
        return c.getTime();
//        System.out.println("增加一天后日期:"+sf.format(c.getTime()));
    }

    /**
     * 获取当前月第一天
     */
    public static String getFirstDayOfMonth() {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");

        Calendar c = Calendar.getInstance();

        c.add(Calendar.MONTH, 0);

        c.set(Calendar.DAY_OF_MONTH,1);//1:本月第一天

        String day1= format.format(c.getTime());

        System.out.println("本月第一天:"+day1);
        return day1;
    }
    /**
     * 获取当前月最后一天
     */
    public static String getLastDayOfMonth() {
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        String day2 = format.format(ca.getTime());
        System.out.println("本月最后一天:" + day2);
        return day2;
    }


    public static void main(String[] args) {
//        Date dayAdd = getDayAdd(1);
//        System.out.println(DateToStringHMSHI(dayAdd));
//        System.out.println(DateToStringHMSHI(new Date()));
        System.out.println(DateToStringY_M_D(new Date()));

//        Date dayOfWeek = getDayOfWeek(1, 2, -1);
//        Date dayOfWeek2 = getDayOfWeek(2, 2, -1);
//        System.out.println(DateToStringHMSHI(dayOfWeek));
//        System.out.println(DateToStringHMSHI(dayOfWeek2));
//        System.out.println(getSecondsNextEarlyMorning());
//        try {
//            System.out.println(dayForWeek(new Date()));
//            System.out.println(dayForWeek(new Date()));
//        } catch (Throwable throwable) {
//            throwable.printStackTrace();
//        }
//        String param = "{\"msgtype\": \"text\",\"text\": {\"content\": \"我是来测试通知告诉你们有一条新的带回复帖子。\"}}";
//        if(DateUtil.isTuiSongAddPublish(StringToDateYYYYMMDDHH("2020-11-01 09:00:01"))){
//            HttpClient.doPost(Constants.tuiSongUrlTest, param);
//        }
//        System.out.println(secToTime(485));
//        Date date = DateAddCountDay(new Date(), -1);
//        System.out.println(DateToStringHMSHI(date));
//        System.out.println(secToTime(485));
//        ArrayList<String> test = test(7);
//        test.forEach(System.out::println);
//        System.out.println(getFetureDate(1));
//        System.out.println(dateToWeek(getFetureDate(1)));
//        System.out.println(StringToDateHHMMSS("08:00:00"));
//        System.out.println(new Time(160000));
//        Date time = new Date(8,0,0);
//        System.out.println(time.getTime());
//        Date date = StringToDateHHMMSS("08:00:00");
//        System.out.println(DateToStringHMSHI(date));
    }

}
